package com.bookyourtrek.action;

import java.math.BigInteger;
import java.sql.SQLException;
import java.util.Map;

import com.bookyourtrek.model.LoginModel;
import com.bookyourtrek.model.OrderModel;
import com.bookyourtrek.model.RegisterModel;
import com.bookyourtrek.util.RuntimeSettings;
import com.bookyourtrek.util.UtilityFunctions;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class PaymentAction extends ActionSupport{

	/**
	 * 
	 */
	private String fullname;
	private String email;
	private String phone;
	private String address;
	
	
	private BigInteger ticketid;
	private double totalprice;
	private Integer selectedquantity;
	private String tickettype;
	

	public String getTickettype() {
		return tickettype;
	}

	public void setTickettype(String tickettype) {
		this.tickettype = tickettype;
	}

	public String getFullname() {
		return fullname;
	}

	public void setFullname(String fullname) {
		this.fullname = fullname;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public BigInteger getTicketid() {
		return ticketid;
	}

	public void setTicketid(BigInteger ticketid) {
		this.ticketid = ticketid;
	}

	public double getTotalprice() {
		return totalprice;
	}

	public void setTotalprice(double totalprice) {
		this.totalprice = totalprice;
	}

	public Integer getSelectedquantity() {
		return selectedquantity;
	}

	public void setSelectedquantity(Integer selectedquantity) {
		this.selectedquantity = selectedquantity;
	}


	private static final long serialVersionUID = 1L;
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String payment () throws SQLException {
		
		//validation starts here
		// for null value
		if(fullname == null || fullname.equalsIgnoreCase("")) {
			addActionError("Full Name Required");
			return INPUT;
		}
		if(email == null || email.equalsIgnoreCase("")) {
			addActionError("Email Required");
			return INPUT;
		}
		if(phone == null || phone.equalsIgnoreCase("")) {
			addActionError("Phone Required");
			return INPUT;
		}
		if(address == null || address.equalsIgnoreCase("")) {
			addActionError("Address Required");
			return INPUT;
		}
		
		// for email
		if (!UtilityFunctions.validateEmail(email)) {
			addActionError("Email Invalid");
			return INPUT;
		}
		// for phone
		if(phone.length() < 10) {
			addActionError("Phone Invalid");
			return INPUT;
		}	
		// validation ends here
		
		//create user account if not exist
		LoginModel loginmodel = new LoginModel();
		if(!loginmodel.checkEmail(email)){
			//RegisterModel object
			RegisterModel registermodel  = new RegisterModel();
			//get a random string
			String randompasscode = UtilityFunctions.randomStringGenerator();
			//register new user
			registermodel.registerNewUser(email,UtilityFunctions.generateMD5(randompasscode), phone);
			//send mail to user about new account
		}
		
			//create order entry
			OrderModel ordermodel = new OrderModel();
			
		
		
			//forward to payment processing
			//check for ticket type, if free process, send mail to user about order
			if (tickettype.equalsIgnoreCase(RuntimeSettings.TKTFREE)) {
				//create order entry
				ordermodel.createOrder(ticketid, fullname, email, address, 
						selectedquantity, totalprice, phone, RuntimeSettings.ORDERCONFIRMED);
				
				//send mail to user
				return SUCCESS;
			}
			//if paid or social cause, forward to payment gateway and 
			//send mail to user about order
			else if (tickettype.equalsIgnoreCase(RuntimeSettings.TKTPAID) ||
					tickettype.equalsIgnoreCase(RuntimeSettings.TKTSOCIAL)) {
				//put order id into session
				BigInteger orderid =  ordermodel.createOrder(ticketid, fullname, email, address, 
						selectedquantity, totalprice, phone, RuntimeSettings.ORDERPAYMENTPENDING);
				Map sess=ActionContext.getContext().getSession();
				sess.put("orderid", orderid);
				
				//forward to payment gateway and mail to user
				
				//send mail to user
				return "payment";
			}
			
			return SUCCESS;
		}
}